#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define Size 15

void InitBoard(char(*Board)[Size])
{
    for(int i = 0; i < Size; i++)
    {
        for(int j = 0; j < Size; j++)
        {
            Board[i][j] = '-';

        }
    }

}

void PrintBoard(char (*Board)[Size])
{
    printf("    ");
    for(int i = 0; i < Size; i++)
    {
        printf("%4d", i + 1);
    }
    printf("\n");
    for(int i = 0; i < Size; i++)
    {
        printf("%4d", i + 1);
        for(int j = 0; j < Size; j++)
        {
            printf("%4c",Board[i][j]);
        }
        printf("\n");
    }
}

bool MakeMove(char (*Board)[Size], int row, int column, char player)
{
    if(row < 0 || row >= Size || column < 0 || column >= Size)
    {
        printf("下的位置是非法位置，请重新输入！");
        return false;
    }
    if(Board[row][column] != '-')
    {
        printf("当前位置已经有棋子了，请重新输入！\n");
        return false;
    }
    Board[row][column] = player;
    return true;
}

bool CheckIsDraw(char(*Board)[Size])
{
    for(int i = 0; i < Size; i++)
    {
        for(int j = 0; j < Size;j++)
        {
            if(Board[i][j] == '-')
            return false;
        }
    }
    return true;//平局
}

bool CheckIsWin(char (*Board)[Size],int row, int column, char player)
{
    //向量数组
    int direction[4][2] = 
    {
        {1,0},
        {0,1},
        {1,1},
        {-1,1}
    };
    //对四个方向继续进行遍历
    for(int i = 0; i < 4; i++)
    {
        //计算这个方向上棋子的数量
        //！！！！！！！向量四个方向
        int count = 1;
        int dx = row + direction[i][0];//在x轴上向右移动
        int dy = column + direction[i][1];//在y轴上向上移动
        while(dx >= 0 && dx < Size && dy >= 0 && dy < Size && player == Board[dx][dy])
        {
            count++;
            dx = dx + direction[i][0];
            dy = dy + direction[i][1];
            if(count == 5)
            {
                return true;
            }
        }
        //怎么把两边数字一起加起来的呢
        dx = row - direction[i][0];
        dy = column - direction[i][1];
        while(dx >= 0 && dx < Size && dy >= 0 && dy < Size && player == Board[dx][dy])
        {
            count++;
            dx = dx - direction[i][0];
            dy = dy - direction[i][1];
            if(count == 5)
            {
                return true;
            }
        }
    }
    return false;
}

int main(int argc,char *argv[])
{
    if(argc != 3)//为何是3
    {
        printf("输入的参数数量错误！\n");
        return -1;
    }
    if(strcmp(argv[1],"admin") == 0 && strcmp(argv[2],"123456") == 0)
    {
        printf("登录成功！欢迎来到五子棋！\n");
    }
    else
    {
        printf("账号或密码错误！\n");
        return -1;
    }
//终端输出以空格隔开
    char Board[Size][Size];
    InitBoard(Board);
    //PrintBoard(Board);
    char player = 'X';
    while(1)
    {
        PrintBoard(Board);
        int row = 0; 
        int column = 0;
        printf("请玩家|%c|下棋：",player);
        //从终端读取输入
        scanf("%d %d",&row,&column);
        if(MakeMove(Board,row - 1, column - 1,player) == false)
        {
            continue;
        }
        if(CheckIsWin(Board,row - 1,column - 1,player) == true)
        {
            PrintBoard(Board);
            printf("恭喜|%c|玩家获胜！\n",player);
            break;
        }
        if(CheckIsDraw(Board) == true)
        {
            PrintBoard(Board);
            printf("平局！\n");
            break;
        }
        player = (player == 'X') ? 'O' : 'X';   
    }
   
    return 0;
}